package com.gewu.ordlelist.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.gewu.ordlelist.R;
import com.gewu.ordlelist.bean.ImageBean;
import com.gewu.ordlelist.utils.jocUtils;
import com.gewu.ordlelist.utils.oneItemClick;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.listener.OnBannerListener;

import java.util.Calendar;
import java.util.Random;


public class oneAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final int FlageOne = 0;
    private final int FlageTwo = 1;
    private oneItemClick oneItemClick;

    public oneAdapter(com.gewu.ordlelist.utils.oneItemClick oneItemClick) {
        this.oneItemClick = oneItemClick;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == FlageOne) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.one_item2, parent, false);
            return new OneItem2(inflate);
        }

        if (viewType == FlageTwo) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.one_item, parent, false);
            return new OneItem(inflate);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof OneItem2) {
            ((OneItem2) holder).banner.setAdapter(new BannerImageAdapter<ImageBean>(ImageBean.getData()) {
                @Override
                public void onBindView(BannerImageHolder holder, ImageBean data, int position, int size) {
                    //图片加载自己实现
                    RoundedCorners roundedCorners = new RoundedCorners(50);
                    RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
                    Glide.with(holder.itemView).load(data.imageRes).apply(options).into(holder.imageView);
                }
            });
            ((OneItem2) holder).banner.setBannerGalleryEffect(50, 20);
            Calendar instance = Calendar.getInstance();
            int month = instance.get(Calendar.MONTH)+1;
            int date = instance.get(Calendar.DATE);
            ((OneItem2)holder).day.setText("0"+month+"/"+date);
            ((OneItem2)holder).banner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(Object data, int position) {
                       oneItemClick.showOnClick2(position,"暂无资料",((OneItem2)holder).banner);
                }
            });
        }

        if (holder instanceof OneItem) {
            RoundedCorners roundedCorners = new RoundedCorners(50);
            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
            int i = new Random().nextInt(6);
            Glide.with(holder.itemView).load(jocUtils.getimgs()[i]).apply(options).into(((OneItem) holder).imageView);
            ((OneItem) holder).textView.setText(jocUtils.getnames()[position-1]);
            ((OneItem) holder).imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    oneItemClick.showOnClick2(position,((((OneItem) holder).textView).getText().toString()),((OneItem)holder).imageView);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return 21;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return FlageOne;
        } else {
            return FlageTwo;
        }
    }

    private class OneItem extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView textView;

        public OneItem(View inflate) {
            super(inflate);
            imageView = inflate.findViewById(R.id.image);
            textView = inflate.findViewById(R.id.text);
        }
    }

    private class OneItem2 extends RecyclerView.ViewHolder {

        Banner banner;
        TextView day;

        public OneItem2(View inflate) {
            super(inflate);
            banner = inflate.findViewById(R.id.banner);
            day = inflate.findViewById(R.id.day);
        }
    }
}
